<!doctype html>
<html xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>图片服务器</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp"/>
  <link rel="icon" type="image/png" href="assets/i/favicon.png">
  <meta name="mobile-web-app-capable" content="yes">
  <link rel="icon" sizes="192x192" href="assets/i/app-icon72x72@2x.png">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="Amaze UI"/>
  <link rel="apple-touch-icon-precomposed" href="assets/i/app-icon72x72@2x.png">
  <meta name="msapplication-TileImage" content="assets/i/app-icon72x72@2x.png">
  <meta name="msapplication-TileColor" content="#0e90d2">
  <link rel="stylesheet" href="assets/css/amazeui.min.css">
  <link rel="stylesheet" href="assets/css/app.css">
  <script src="js/vue.js"></script>
</head>

<body id="blog-article-sidebar">
<!-- header start -->
<header class="am-g am-g-fixed blog-fixed blog-text-center blog-header">
    <div class="am-u-sm-8 am-u-sm-centered">
        <h2 class="am-hide-sm-only">图片服务器</h2>
    </div>
</header>
<!-- header end -->
<hr>

<div id="app">
    <!-- nav start -->
    <nav class="am-g am-g-fixed blog-fixed blog-nav">
    <button class="am-topbar-btn am-topbar-toggle am-btn am-btn-sm am-btn-success am-show-sm-only blog-button" data-am-collapse="{target: '#blog-collapse'}" ><span class="am-sr-only">导航切换</span> <span class="am-icon-bars"></span></button>

      <div class="am-collapse am-topbar-collapse" id="blog-collapse">
        <ul class="am-nav am-nav-pills am-topbar-nav">
        </ul>
          <form class="am-topbar-form am-topbar-right am-form-inline" v-on:submit.prevent="imageUpload()">
              <div class="am-form-group">
                  <input type="file" v-on:change="changeImage($event)" class="am-form-field am-input-sm">
              </div>
              <div class="am-form-group">
                  <input type="submit" class="am-form-field am-input-sm"  style="height:41px" value="上传"/>
              </div>
          </form>
      </div>
    </nav>
    <!-- nav end -->
    <hr>
    <!-- content srart -->
    <div class="am-g am-g-fixed blog-fixed blog-content">
        <figure data-am-widget="figure" class="am am-figure am-figure-default "   data-am-figure="{  pureview: 'true' }">
            <div id="container">
                <div v-for="image in images">
                    <img v-bind:src="'imageShow?imageId=' + image.imageId" style="height:200px; width:200px">
                    <h3 style="margin-left: auto; margin-right: auto;">{{image.imageName}}</h3>
                    <button style="width:100%" class="am-btn am-btn-success" v-on:click.stop="remove(image.imageId)">删除</button>
                </div>
            </div>
        </figure>
    </div>
</div>
<!-- content end -->
<footer class="blog-footer">
    <div class="blog-text-center">© Copyright by 米老鼠 </div>
</footer>


<!--[if (gte IE 9)|!(IE)]><!-->
<script src="assets/js/jquery.min.js"></script>
<!--<![endif]-->
<!--[if lte IE 8 ]>
<script src="js/jquery.min.js"></script>
<script src="js/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->
<script src="assets/js/amazeui.min.js"></script>
<script src="assets/js/pinto.min.js"></script>
<script src="assets/js/img.js"></script>
<script>
    var app = new Vue({
        el:'#app',
        data: {
            images: [
            ],
            uploadImage: ''
        },
        methods: {
            getImages() {
                $.ajax({
                    url: "image",
                    type: "get",
                    context: this,
                    success: function(data, status) {
                        this.images = data;
                        $("#app").resize();
                    }
                })
            },
            changeImage(event){
                app.uploadImage = event.target.files[0];
            },
            imageUpload(){
                if(!app.uploadImage) {
                    alert("选择图片后上传");
                    return;
                }
                let data = new FormData();
                data.append("uploadImage", app.uploadImage);
                $.ajax({
                    url: "image",
                    type: "post",
                    processData: false,
                    contentType: false,
                    data: data,
                    // context: this,
                    success: function(data, status) {
                        if(data.ok){
                        app.getImages();
                        alert("上传成功");}
                        else{
                            alert("上传失败:"+data.msg)
                        }

                    },
                    error: function (err, textStatus, throwable) {
                        console.error(err)
                    }
                })
            },
            remove(imageId) {
                $.ajax({
                    url:"image?imageId=" + imageId,
                    type:"delete",
                    context: this,
                    success: function(data, status) {
                        app.getImages();
                        alert("删除成功");
                    }
                })
            }
        },
    });

    app.getImages();
</script>
</body>
</html>